package com.qkl.xiatang.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qkl.xiatang.app.entity.user.AppUserIdentityEntity;
import com.qkl.xiatang.app.entity.user.AppUserInfo;
import com.qkl.xiatang.app.mapper.AppUserIdentityMapper;
import com.qkl.xiatang.app.utils.AppUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

@Service
public class AppUserIdentityService {

    @Autowired
    private AppUserIdentityMapper appUserIdentityMapper;

    public List<AppUserIdentityEntity> queryUserIdentityList(String companyId,String userId){
        return appUserIdentityMapper.selectList(new QueryWrapper<AppUserIdentityEntity>()
                .eq("company_id",companyId)
                .eq("app_user_id",userId));
    }

    public Set<String>  queryUserIdentitySet(String companyId,String userId){
        Set<String> set = new TreeSet<>();
        List<AppUserIdentityEntity> list = this.queryUserIdentityList(companyId,userId);
        if(ObjectUtils.isNotEmpty(list)){
            for (AppUserIdentityEntity entity : list){
                set.add(String.valueOf(entity.getIdentityId()));
            }
        }
        return set;
    }

    public void queryUserIdentitySet(String companyId,String userId, AppUserInfo userInfo){
        Set<String> set = new TreeSet<>();
        List<AppUserIdentityEntity> list = this.queryUserIdentityList(companyId,userId);
        if(ObjectUtils.isNotEmpty(list)){
            for (AppUserIdentityEntity entity : list){
                set.add(String.valueOf(entity.getIdentityId()));
            }
            userInfo.setAppUserIdentityEntities(list);
        }

        String identity = String.join(",", set);
        if(StringUtils.isNotEmpty(identity)){
            userInfo.setIdentity(identity);
       }


    }

    public String queryUserIdentityString(String companyId,String userId){
        Set<String> set = this.queryUserIdentitySet(companyId,userId);
        return String.join(",",set);
    }

    public void queryUserIdentityString(String companyId,String userId, AppUserInfo userInfo){
       this.queryUserIdentitySet(companyId, userId, userInfo);
    }

    /**
     * 更新用户身份
     * @param companyId
     * @param userId
     * @param identityStrings
     */
    @Transactional
    public void updateUserIdentity(String companyId,String userId,String identityStrings){
        List<String> identityList = AppUtils.stringToList(identityStrings);
        if(ObjectUtils.isNotEmpty(identityList)){
            appUserIdentityMapper.delete(new QueryWrapper<AppUserIdentityEntity>()
                                            .eq("company_id",companyId)
                                            .eq("app_user_id",userId));
            this.addUserIdentityList(companyId,userId,identityStrings);
        }
    }

    /**
     * 添加用户身份
     * @param companyId
     * @param userId
     * @param identityStrings
     */
    public void addUserIdentityList(String companyId,String userId,String identityStrings){
        List<String> identityList = AppUtils.stringToList(identityStrings);
        if(ObjectUtils.isNotEmpty(identityList)){
            for(String identity : identityList) {
                this.addUserIdentity(companyId,userId,identity);
            }
        }
    }

    private String addUserIdentity(String companyId,String userId,String identity){
        return this.addUserIdentity(companyId,userId, AppUtils.parseInt(identity));
    }
    private String addUserIdentity(String companyId,String userId,Integer identity){
        AppUserIdentityEntity entity = new AppUserIdentityEntity();
        entity.setAppUserId(userId);
        entity.setCompanyId(companyId);
        entity.setIdentityId(identity);
        entity.setCreateTime(new Date());
        appUserIdentityMapper.insert(entity);
        return entity.getId();
    }

}
